﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace Projektet.View
{
    class CatapultView : Model.ExplosionListener
    {
        private Model.Catapult _catapult;

        private SpriteFont _spriteFont;

        private Texture2D _catapultTexture;
        private Texture2D _projectileTexture;
        private Texture2D _powerBar;
        private Texture2D _powerBarOutline;

        private CustomSystem _customSystem;

        public CatapultView(Model.Catapult catapult) 
        {
            _catapult = catapult;
        }

        public void LoadContent(ContentManager contManager) 
        {
            _catapultTexture = contManager.Load<Texture2D>("testCatapult");
            _projectileTexture = contManager.Load<Texture2D>("RockFragment");

            _powerBar = contManager.Load<Texture2D>("catapultPowerbar");
            _powerBarOutline = contManager.Load<Texture2D>("catapultPowerbarOutline");

            _spriteFont = contManager.Load<SpriteFont>("SpriteFont1");
        }
        //Den visar inte deras riktiga positioner!
        public void Draw(SpriteBatch spriteBatch)
        {
            DrawUi(spriteBatch);

            Rectangle visualRect = Camera.GetVisualRectangle(new Rectangle(
                (int)_catapult._position.X,
                (int)(_catapult._position.Y),
                _catapult._width,
                _catapult._height));
            
            spriteBatch.Draw(_catapultTexture, visualRect, Color.White);

            foreach(Model.CatapultShot  cShot in _catapult._projectileList)
            {
                Rectangle projectileVisualRect = Camera.GetVisualRectangle(new Rectangle(
                (int)cShot._position.X,
                (int)(cShot._position.Y),
                cShot._width,
                cShot._height));

                float rotation = cShot._duration / 72;
                //Vector2 origin = new Vector2((cShot._width ), (cShot._height ));
                Vector2 origin = Camera.GetVisualVector(new Vector2(cShot._width, cShot._height));

                spriteBatch.Draw(_projectileTexture, projectileVisualRect, null, Color.White, rotation, origin, SpriteEffects.None, 0.2f);
            }

            if(_customSystem != null)
            {
                _customSystem.Draw(spriteBatch);
            }
        }

        public void CreateExplosion(Vector2 impactPosition) 
        {
            _customSystem = new CustomSystem(impactPosition, _projectileTexture);
        }

        public void DrawUi(SpriteBatch spriteBatch)
        {
            //string gold = "Gold: " + _catapult._cooldown;
            Vector2 firstRow = new Vector2(10, 50);
            Vector2 secondRow = new Vector2(10, 80);
            Color color = Color.White;
            int width = 150;
            int height = 20;
            int rectXOffset = 100;

            Rectangle powerRectFull = new Rectangle((int)firstRow.X + rectXOffset, (int)firstRow.Y, width, height);
            Rectangle powerRect = new Rectangle((int)firstRow.X + rectXOffset, (int)firstRow.Y, (int)(width * _catapult._power), height);
            spriteBatch.DrawString(_spriteFont, "Power: ", firstRow, color);
            spriteBatch.Draw(_powerBar, powerRect, color);
            spriteBatch.Draw(_powerBarOutline, powerRectFull, color);

            Rectangle cdRectFull = new Rectangle((int)secondRow.X + rectXOffset, (int)secondRow.Y, width, height);
            Rectangle cdRect = new Rectangle((int)secondRow.X + rectXOffset, (int)secondRow.Y, (int)(width * _catapult._cooldown), height);
            spriteBatch.DrawString(_spriteFont, "Cooldown: ", secondRow, color);
            spriteBatch.Draw(_powerBar, cdRect, color);
            spriteBatch.Draw(_powerBarOutline, cdRectFull, color);
        }
    }
}
